<?php
namespace application\datatypes;
use application\datatypes\DataType;
use application\utilities\Utility;
class Author extends DataType {
	public static $name = 'Author';
	public static $fields = array(array('columnType'=>'BIGINT(20)'));
	
	
	public static function validate($field, $data, $results) {
		global $concerto;
		
		if(!$data['content_author']) {
			$results['db_fields']['content_author'] = $_SESSION['cn_user_id'];
		}
		else {
			// Make sure it's an ID of an existing user
			/*$user = DB()->getVar("SELECT `user_id` FROM `cn_users` WHERE `user_id`='".$data['content_author']."'");
			if(!$user) {
				$results['errors'][] = 'Content author must be the user ID of an existing user.';
			}
			else {*/
				$results['db_fields']['content_author'] = $data['content_author'];
			//}
		}
		
		
		return $results;
	}
	
	public static function display($formElements) {
		if($formElements) {
			echo $formElements['elements']['element'];
		}
	}
	
	public static function build($field, $row) {
		return FALSE;
	
	}
	
	public static function editForm($field) {
		?>
		<div class="box-content">
			You can't edit an automatic field.
		</div>
			<?php
	}
	
	public static function prepareData($field, $data, $params) {
		$author = $data[$field->cf_column];
		if($params['view'] == 'id') {
			return $author;
		}
		else {
			// Get display name
			$user = new \application\models\User($author);
			$return = $user->displayName();
			unset($user);
			return $return;
		}
	}
	
	
	
}
?>